﻿
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace SpeedSearch
{
    [XmlRoot("Config")]
    [XmlInclude(typeof(Virtual))] // include type class Person
    public class Config
    {
        /// <summary>
        /// singleton d'accès à la configuration
        /// </summary>
        private static Config _instance = null;

        public static Config GetInstance()
        {
            _instance = (_instance == null) ? new Config() : _instance;
            return _instance;
        }

        private Config()
        {
            this.Virtualization = new List<Virtual>();
        }

        public void LoadConfig()
        {
            XmlSerializer x = new XmlSerializer(GetType());
            using (var s = new StreamReader("Config.xml"))
            {
                _instance = (Config)x.Deserialize(s);
            }
        }

        public void SaveConfig()
        {
            XmlSerializer x = new XmlSerializer(GetType());
            using (var s = new StreamWriter("Config.xml", false))
            {
                x.Serialize(s, _instance);
            }
        }

        #region SerializableMembers

        [XmlArray("Virtualization")]
        [XmlArrayItem("Virtual")]
        public List<Virtual> Virtualization { get; set; }

        #endregion
    }
}
